/*
 * ApagaRevistaAction.java
 *
 * Created on 25 de Julho de 2007, 11:41
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.RevistaDAO;
import br.usp.poli.mfc.struts.actionform.ConsultaRevistaActionForm;
import br.usp.poli.mfc.struts.bean.Usuario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 *
 * @author Renato
 * @version
 */

public class ApagaRevistaAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERROR = "error";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ConsultaRevistaActionForm frmConsRev = (ConsultaRevistaActionForm) form;
        HttpSession xsession = request.getSession();
        Usuario user = (Usuario)xsession.getAttribute("usuario");
        RevistaDAO _revistaDAO = DAOFactory.getRevistaDAO();
        if(user.getId_cargo() == 6){
            if(_revistaDAO.deleteRevistaByID(frmConsRev.getIdRevista())){
                ActionMessages ok = new ActionMessages();
                ok.add("OK",new ActionMessage("apagarevista.success"));
                saveErrors(request,ok);
                return mapping.findForward(SUCCESS);
            } else {
                ActionMessages nok = new ActionMessages();
                nok.add("NOK",new ActionMessage("error.apagarevista.error"));
                saveErrors(request,nok);
                return mapping.findForward(ERROR);
            }
        }
        ActionMessages nok = new ActionMessages();
        nok.add("Unauthorized",new ActionMessage("error.apagarevista.naoautorizado"));
        saveErrors(request,nok);
        return mapping.findForward(ERROR);
        
    }
}
